<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<style>
</style>

<div class="add">
    <h3>新闻列表</h3>
    <table border="1px" >
        <thead>
            <tr>
                <th>标题</th>
                <th>作者</th>
                <th>操作</th>
            </tr>

        </thead>
        <tbody id="newstbody">

        </tbody>
    </table>
    <div class="pageInfo">
        当前页：<span id="currPageNo"></span>
        总页：<span id="totalPageCount"></span>
        每页多少记录：<span id="pageSize"></span>
        总记录数：<span id="totalCount"></span>
        <br/>
        <div id="changePage">

        </div>

    </div>
</div>

<script>
    $(document).ready(function(){
        $.ajax({
            type: "POST",
            url: "${ctx}/MainNews?oper=mainnewsPageListAjax",
            data: {
                currPageNo:<%=request.getParameter("currPageNo")%>//当前页
            },
            dataType: "json",//预期服务器返回的数据类型。"json": 返回 JSON 数据 。
            success: function(msg){
                console.log(msg)
                let objectList = msg.objectList;
                let newsRes = '';
                $.each( objectList, function(i, item){
                        newsRes += '<tr>';
                        newsRes += '    <td>'+item.title+'</td>';
                        newsRes += '    <td>'+item.userid+'</td>';
                        newsRes += '    <td>';
                        newsRes += '        <a href="http://xxx/editNews?id='+item.id+'">编辑</a>';
                        newsRes += '        <a href="http://xxx/delNews?id='+item.id+'">删除</a>';
                        newsRes += '    </td>';
                        newsRes += '</tr>';
                });
                $("#newstbody").html(newsRes);

                $('#currPageNo').html(msg.currPageNo);
                $('#pageSize').html(msg.pageSize);
                $('#totalCount').html(msg.totalCount);
                $('#totalPageCount').html(msg.totalPageCount);
                let changePage = '';
                let totalPageCount = Number(msg.totalPageCount);
                for (let i = 1; i <= totalPageCount; i++) {
                    if(i == 1){
                        changePage += ' <a href="${ctx}/news/news_main/mainnews-list.jsp?currPageNo=1">首页</a>';
                    }
                    changePage += ' <a href="${ctx}/news/news_main/mainnews-list.jsp?currPageNo='+i+'">'+i+'</a>';
                    if(i == totalPageCount){
                        changePage += ' <a href="${ctx}/news/news_main/mainnews-list.jsp?currPageNo='+totalPageCount+'">尾页</a>';
                    }
                }
                $("#changePage").html(changePage);
            }
        });
    });
</script>